import os
import sys
import json
import platform
import traceback
from datetime import datetime, timedelta



class GlobalInfo(object):
    server_home = os.path.abspath(os.path.dirname(sys.argv[0]))
    website_address = ''
    is_multi_process_mode = True
    
    initdone = False
    
    @staticmethod
    def show():
        return f"GlobalInfo.server_home = {GlobalInfo.server_home}\n" + \
               f"GlobalInfo.webaddres = {GlobalInfo.webaddres}"


class GlobalCfg(object):
    workspace = None

    # web
    web_ip = '127.0.0.1'
    web_port = '45678'
    
    # module master
    force_thread_mode = False

    # sigalarm
    sigalarm_cfg = None
    
    # knowledge lib
    knowledgelib_cfg = None
    
    # gitee id
    giteeid_cfg = None

    @staticmethod
    def init(config_file='config.json'):
        assert not GlobalInfo.initdone
        
        with open(config_file, 'r') as f:
            config = json.load(f)
        
        for key in config:
            exec(f'GlobalCfg.{key} = config["{key}"]')
        
        if GlobalCfg.workspace is None or GlobalCfg.workspace == "":
            GlobalCfg.workspace = os.path.join(GlobalInfo.server_home, 'workspace')
        if not os.path.exists(GlobalCfg.workspace):
            os.makedirs(GlobalCfg.workspace)
        
        GlobalInfo.website_address = f'http://{GlobalCfg.web_ip}:{GlobalCfg.web_port}'
        GlobalInfo.initdone = True
        GlobalInfo.is_multi_process_mode = (platform.system() == 'Linux' and not GlobalCfg.force_thread_mode)
        

    @staticmethod
    def show():
        return f"GlobalCfg.workspace = {GlobalCfg.workspace}\n" + \
               f"GlobalCfg.web_ip = {GlobalCfg.web_ip}" + \
               f"GlobalCfg.web_port = {GlobalCfg.web_port}" + \
               f"GlobalCfg.xiaolb_server = {GlobalCfg.xiaolb_server}" + \
               f"GlobalCfg.xiaolb_auth = {GlobalCfg.xiaolb_auth}" + \
               f"GlobalCfg.sigalarm_cfg = {GlobalCfg.sigalarm_cfg}"

